﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using LuxuryResort.Entity;

namespace LuxuryResort.DAO
{
    public class FoodBeverageDAO : BaseDAO
    {
        public List<Food_Beverage> GetAll(Guid userId, Guid resortId)
        {
            var lst = new List<Food_Beverage>();
            List<LRMPConnection> cs = GetConnectionString(userId, resortId);
            if (cs.Count > 0)
            {
                using (var context = new LRMPEntities(cs[0].ConnectionString))
                {
                    lst.AddRange(context.Food_Beverage.ToList());
                }
            }
            return lst;
        }

        public Food_Beverage GetFBById(Guid id, Guid userId, Guid resortId)
        {
            Food_Beverage fb = null;
            List<LRMPConnection> cs = GetConnectionString(userId, resortId);
            if (cs.Count > 0)
            {
                using (var context = new LRMPEntities(cs[0].ConnectionString))
                {
                    fb = context.Food_Beverage.FirstOrDefault(f => f.FID == id);
                }
            }
            return fb;
        }

        public int InsertFood(Food_Beverage food, Guid userId, Guid resortId)
        {
            List<LRMPConnection> cs = GetConnectionString(userId, resortId);
            if (cs.Count > 0)
            {
                using (var context = new LRMPEntities(cs[0].ConnectionString))
                {
                    context.Food_Beverage.AddObject(food);
                    return context.SaveChanges();
                }
            }

            return 0;
        }

        public int UpdateFood(Food_Beverage food, Guid userId, Guid resortId)
        {
            List<LRMPConnection> cs = GetConnectionString(userId, resortId);
            if (cs.Count > 0)
            {
                using (var context = new LRMPEntities(cs[0].ConnectionString))
                {
                    context.AddObject("Food_Beverage", food);
                    context.ObjectStateManager.ChangeObjectState(food, EntityState.Modified);

                    return context.SaveChanges();
                }
            }

            return 0;
        }

        public int DeleteFood(Guid foodId, Guid userId, Guid resortId)
        {
            List<LRMPConnection> cs = GetConnectionString(userId, resortId);
            if (cs.Count > 0)
            {
                using (var context = new LRMPEntities(cs[0].ConnectionString))
                {
                    IQueryable<Food_Beverage_Order> subItems = context.Food_Beverage_Order.Where(x => x.FID == foodId);
                    foreach (Food_Beverage_Order order in subItems)
                    {
                        context.Food_Beverage_Order.DeleteObject(order);
                    }
                    context.SaveChanges();

                    var item = context.Food_Beverage.FirstOrDefault(x => x.FID == foodId);
                    //if (item != null) context.ObjectStateManager.ChangeObjectState(item, EntityState.Deleted);
                    //context.AttachTo(context.GetType().Name, item);
                    //context.ObjectStateManager.ChangeObjectState(item, EntityState.Deleted);
                    context.DeleteObject(item);
                    return context.SaveChanges();
                }
            }

            return 0;
        }
    }
}